package com.example.wangfanwangluo.wangfanboss.PublicDirectory;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

public class functions {
    //所有工具方法
    /*
    * 获取组件的宽度和高度
    * @param view 需要获取宽度高度的view
    * @return int[] arr[0]表示宽度，arr[1]表示高度
    * */
    public static int[] getWH(View v){
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        int width = v.getMeasuredWidth();
        int height = v.getMeasuredHeight();
        int[] arr = new int[2];
        arr[0] = width;
        arr[1] = height;
        return arr;
    }
    /**
     * 判断是否是银行卡号
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId
                .substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }
    private static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null
                || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }
    /*
    * 关闭键盘
    * @param context
    * @return void
    * */
    public static void closeKeyboard(Context context){
        InputMethodManager inputMethodManager = (InputMethodManager)
                context.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (inputMethodManager.isActive()){
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /*
    * 校验手机号是否正确
    * @param phoneNum 手机号
    * @return boolean
    * */
    public static boolean phoneCheck(String phoneNum){
        return phoneNum.matches("^[1][3578][0-9]{9}$");
    }

    /*
    * 校验密码
    * @param pw 密码
    * @return boolean
    * */
    public static boolean pwCheck(String pw){
        return pw.matches("^[a-zA-Z0-9]{6,16}$");
    }

    /*
    * 校验短信验证码
    * @param code 验证码
    * @return boolean
    * */
    public static boolean codeCheck(String code){
        return code.matches("\\d{6}");
    }

    /*
    * 获取验证码加密
    * @param phoneNum 需要加密的手机号
    * @return result 返回加密后的手机号
    * */
    public static String safeEncode(String phoneNum){
        phoneNum = reverse(phoneNum);
        long phone = Long.valueOf(phoneNum).longValue();
        phone = phone * 2;
        String str = String.valueOf(phone);
        String result = new String();
        char[] checklist = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
        for (int i = 0; i < str.length(); i++) {
            String tmp = str.substring(i, i+1);
            int index = Integer.parseInt(tmp);
            result += checklist[index];
        }
        return result;
    }

    /*
    * 字符串反转
    * @param str 需要反转的字符串
    * @return String 反转后的结果
    * */
    private static String reverse(String str){
        char[] chars = str.toCharArray();
        String result = "";
        if (chars.length <= 1){
            result = new String(chars);
        } else {
            for (int i = chars.length - 1 ; i >= 0; i--){
                result += chars[i];
            }
        }
        return result;
    }

    /*
    * 从本地获取获取token
    * @param context 上下文
    * @return token
    * */
    public static String getToken(Context context){
        SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String token = preferences.getString("token", "");
        return token;
    }

    /*
    * 从本地获取获取手机号
    * @param context 上下文
    * @return mobile
    * */
    public static String getMobile(Context context){
        SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String mobile = preferences.getString("mobile", "");
        return mobile;
    }

    /*
    * 两种图片选一种显示，另外一种隐藏
    * @param view1 需要显示的图片
    * @param view2 需要隐藏的图片
    * @return void
    * */
    public static void eitherOrImageView(View view1, View view2){
        view1.setVisibility(View.VISIBLE);
        view2.setVisibility(View.GONE);
    }

    /*
    * 任意张图片需要隐藏
    * @param views 需要隐藏的图片
    * @return void
    * */
    public static void hideAllImageView(View... views){
        for (int i = 0; i < views.length; i++){
            views[i].setVisibility(View.GONE);
        }
    }

    /*
    * 找到字符串中不是空的那一个，并返回，若全部都是空，则返回空字符串
    * @param str 需要查找的字符串
    * @return result 查找的结果
    * */
    private static String[] choseOne(String... str){
        String result = "";
        int index = 0;
        for (int i = 0; i < str.length; i++){
            if (!TextUtils.isEmpty(str[i])){
                result = str[i];
                index = i;
                break;
            }
        }
        String[] arr = new String[2];
        arr[0] = result;
        arr[1] = String.valueOf(index);
        return arr;
    }

    /*
    * 获取运单状态对应的汉字
    * $param state 运单状态的数字
    * $return result 运单状态的汉字
    * */
    public static String getOrderState(int state){
        String result = "";
        switch (state){
            case 1:
                result = "未配载";
                break;
            case 2:
                result = "已配载";
                break;
            case 3:
                result = "已发车";
                break;
            case 4:
                result = "已到达";
                break;
            case 5:
                result = "已完成";
                break;
            default:
                break;
        }
        return result;
    }

    /*
    * 发货运单、到货运单列表页，获取货物名称和货物大小相关
    * @param goods_name1, goods_name2, goods_name3: 货物名称
    * @param goods_weight1, String goods_weight2, String goods_weight3: 货物重量
    * @param goods_volume1, goods_volume2, goods_volume3: 货物体积
    * @param goods_num1, goods_num2, goods_num3: 货物数量
    * @return result   result[0]: 货物名称    resutl[1]: 货物大小相关
    * */
    public static String[] getGoods(String goods_name1, String goods_weight1, String goods_volume1, String goods_num1,
                                    String goods_name2, String goods_weight2, String goods_volume2, String goods_num2,
                                    String goods_name3, String goods_weight3, String goods_volume3, String goods_num3){
        String goods_name = "";
        String goods_weight = "";
        String goods_volume = "";
        String goods_num = "";
        String[] result = new String[2];

        String[] goodsResult = functions.choseOne(goods_name1, goods_name2, goods_name3);
        int index = Integer.parseInt(goodsResult[1]);
        switch (index){
            case 0:
                goods_name = goods_name1;
                goods_weight = goods_weight1;
                goods_volume = goods_volume1;
                goods_num = goods_num1;
                break;
            case 1:
                goods_name = goods_name2;
                goods_weight = goods_weight2;
                goods_volume = goods_volume2;
                goods_num = goods_num2;
                break;
            case 2:
                goods_name = goods_name3;
                goods_weight = goods_weight3;
                goods_volume = goods_volume3;
                goods_num = goods_num3;
                break;
            default:
                break;
        }
        String goods_size = "";
        if (TextUtils.isEmpty(goods_weight) && TextUtils.isEmpty(goods_volume) && TextUtils.isEmpty(goods_num)){
            goods_size = "";
        } else {
            goods_size += "(";
            if (!TextUtils.isEmpty(goods_weight)){
                goods_size += goods_weight + "吨";
            }

            if (!TextUtils.isEmpty(goods_volume)){
                if (!TextUtils.isEmpty(goods_weight)){
                    goods_size += "," + goods_volume + "方";
                } else {
                    goods_size += goods_volume + "方";
                }

            }

            if (!TextUtils.isEmpty(goods_num)){
                if (!TextUtils.isEmpty(goods_weight) || !TextUtils.isEmpty(goods_volume)){
                    goods_size += "," + goods_num + "件";
                } else {
                    goods_size += goods_num + "件";
                }
            }
            goods_size += ")";
        }
        result[0] = goods_name;
        result[1] = goods_size;
        return result;
    }

    public static void selectOrderState(String state, TextView textView, View... views){
        textView.setText(state);
        views[0].setVisibility(View.VISIBLE);
        views[1].setVisibility(View.GONE);
        views[2].setVisibility(View.GONE);
    }

    public static void cancelOrderState(View... views){
        views[0].setVisibility(View.GONE);
        views[1].setVisibility(View.VISIBLE);
        views[2].setVisibility(View.VISIBLE);
    }
}
